<?php

/**
 * Description of Promotion_media
 *
 * @author Administrator
 */
class Promotion_media extends CI_Controller {

  protected $class;
  protected $model;
  public $base_lib;

  public function __construct() {
    parent::__construct();    
    $this->class = $this->router->fetch_class();
    $model_name = $this->class . "_model";
    $this->model = new $model_name();
    $this->base_lib = new Base_lib();
  }

  public function index() {
    redirect('admin/promotion');
  }

  public function show($id = '') {
    if (!userdata('username')) {
      redirect('admin/login');
      return;
    }
    if (!empty($id)) {
      $obj = $this->model->get($id);
      if (!empty($obj)) {
        $this->load->view('admin/' . $this->class . '/show', array('obj' => $obj));
        return;
      }
    }
    set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.' . $this->class), $id)));
    redirect('admin/'.$this->class);
  }

  public function create($promotion_id = 0) {
    if (!userdata('username')) {
      redirect('admin/login');
      return;
    }
    $promotion = $this->promotion_model->get($promotion_id);
    if ($promotion) {
      $this->load->view('admin/' . $this->class . '/create', array('promotion_id' => $promotion_id));
    } else {
      set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.promotion'), $promotion_id)));
      redirect('admin/promotion');
    }
  }

  public function save($promotion_id = 0) {
    if (!userdata('username')) {
      redirect('admin/login');
      return;
    }
    if (empty($_REQUEST['promotion_id'])) {
      $_REQUEST['promotion_id'] = $promotion_id;
    }
    if (!empty($_REQUEST['promotion_id'])) {
      $_REQUEST['path'] = $this->base_lib->_upload_img('path', 'promotion/' . $_REQUEST['promotion_id']);
    } else
      $_REQUEST['path'] = 'no media';
    $id = $this->model->save($_REQUEST);
    if (is_array($id))
      set_flash_error($id);
    else
    if (!empty($id)) {
      set_flash_message($this->lang->line('message.created', array($this->lang->line('label.' . $this->class), $promotion_id)));
      if (!empty($_REQUEST['btnAction']) && $_REQUEST['btnAction'] == 'save_more') {
        redirect('admin/'.$this->class . '/create/' . $promotion_id);
      } else {
        redirect('admin/promotion/show/' . $promotion_id.'/media');
      }
      return;
    } else
      set_flash_error($this->lang->line('error.faltal.error'));
    $this->load->view('admin/' . $this->class . '/create', array(
        'obj' => $_REQUEST, 'promotion_id' => $_REQUEST['promotion_id']));
  }

  public function edit($id = '', $promotion_id = 0) {
    if (!userdata('username')) {
      redirect('admin/login');
      return;
    }
    $promotion = $this->promotion_model->get($promotion_id);
    if ($promotion) {
      if (!empty($id)) {
        $obj = $this->model->get($id);
        if (!empty($obj)) {
          $this->load->view('admin/' . $this->class . '/edit', array(
              'promotion_id' => $promotion_id, 'obj' => $obj));
          return;
        }
      }
      set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.' . $this->class), $id)));
      redirect('admin/'.$this->class);
    } else {
      set_flash_error($this->lang->line('error.not.found', array($this->lang->line('promotion'), $id)));
      redirect('admin/promotion');
    }
  }

  public function update($promotion_id = 0) {
    if (!userdata('username')) {
      redirect('admin/login');
      return;
    }
    if (empty($_REQUEST['promotion_id'])) {
      $_REQUEST['promotion_id'] = $promotion_id;
    }
    if (!empty($_REQUEST['promotion_id'])) {
      $_REQUEST['path'] = $this->base_lib->_upload_img('path', 'promotion/' . $_REQUEST['promotion_id']);
    } else
      $_REQUEST['path'] = 'no media';
    $id = $this->model->save($_REQUEST);
    if (is_array($id))
      set_flash_error($id);
    else
    if ($id === true) {
      set_flash_message($this->lang->line('message.updated', array($this->lang->line('label.' . $this->class), $_REQUEST['id'])));
      redirect('admin/promotion/show/' . $promotion_id.'/media');
      return;
    } else
      set_flash_error($this->lang->line('error.faltal.error'));
    $this->load->view('admin/' . $this->class . '/edit', array(
        'promotion_id' => $promotion_id,'obj' => $_REQUEST));
  }

  public function delete($id='', $promotion_id='') {
    if (!userdata('username')) {
      redirect('admin/login');
      return;
    }
    if (!empty($id)) {
      $obj = $this->model->get($id);
      if (!empty($obj)) {
        $this->model->delete($id);
        set_flash_message($this->lang->line('message.deleted', array($this->lang->line('label.' . $this->class), $promotion_id)));
        redirect('admin/promotion/' . (empty($promotion_id) ? '' : 'show/' . $promotion_id.'/media'));
        return;
      }
    }
    set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.' . $this->class), $id)));
    redirect('admin/promotion');
  }

  function preview_promotion($promotion_id = '') {
    $promotion_media = $this->model->get(array('promotion_id' => $promotion_id, 'is_main' => true));
    if (!$promotion_media) {
      $promotion_media = $this->model->get(array('promotion_id' => $promotion_id));
    }
    if ($promotion_media) {
      redirect('admin/file_upload/get_img_from_path?path=promotion/' . $promotion_id . '/' . $promotion_media['path']);
    } else {
      redirect('admin/file_upload/get_img_from_path?path=promotion/' . $promotion_id . '/no_image');
    }
  }

  function preview($id = '') {
    $promotion_media = $this->model->get($id);
    if ($promotion_media) {
      redirect('admin/file_upload/get_img_from_path?path=promotion/' . $promotion_media['promotion_id'] . '/' . $promotion_media['path']);
    } else {
      redirect('admin/file_upload/get_img_from_path?path=promotion/' . $promotion_media['promotion_id'] . '/no_image');
    }
  }

}